package com.mikelouis.test.scala.impatient.chapter20_actor

import scala.actors.Actor

/**
 * Created by yuazhu on 6/1/2015.
 */
object Chapter20_6 {

  class ReceiveActor extends Actor {
    def act() {
      while (true) {
        receive {
          case 'Hello => println(this.getClass.getSimpleName + ": " + Thread.currentThread)
        }
      }
    }
  }

  class ReactActor extends Actor{
    override def act(): Unit = {
      loop {
        react {
          case 'Hello => println(this.getClass.getSimpleName + ": " + Thread.currentThread)
        }
      }
    }
  }


  def main(args: Array[String]) {
    // Actors with while/receive
    for (i <- 1 to 100) {
      val actor = new ReceiveActor
      actor.start()
      actor ! 'Hello
    }

    println("*************************************")

    // Actors with loop/react
    for (i <- 1 to 100) {
      val actor = new ReactActor
      actor.start()
      actor ! 'Hello
    }

    println("*************************************")

    println("main: " + Thread.currentThread)
  }
}
